﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Inscriptionem.Services
{
    public static class EncryptionService
    {
        public static string EncryptString(string UserLogin, string Value)
        {
            string computedHashValue;
            UserLogin = UserLogin.ToLower();

            //user name as salt bytes
            // Convert password into a byte array.
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(Value);

            // Convert user name into salt byte array.
            byte[] saltBytes = Encoding.UTF8.GetBytes(UserLogin);

            // Allocate array, which will hold password and username as saltbytes.
            byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];

            // Copy plain text bytes(password) into resulting array.
            Array.Copy(plainTextBytes, plainTextWithSaltBytes, plainTextBytes.Length);

            // Append salt bytes(username) to the resulting array.
            Array.Copy(saltBytes, 0, plainTextWithSaltBytes, plainTextBytes.Length, saltBytes.Length);

            //Create an instance of SHA512Managed class
            using (SHA512Managed hash = new SHA512Managed())
            {
                computedHashValue = Convert.ToBase64String(hash.ComputeHash(plainTextWithSaltBytes));
            }

            //Returns Compute hash value
            return computedHashValue;
        }
    }
}